/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsSupplierContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseSupplierInfo;
import com.xteam.ecsystem.service.admin.GoodsSupplierService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 供货商控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class GoodsSupplierContorller {

	@Autowired
	private GoodsSupplierService supplierService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getGoodsSupplierList.do")
	public void getGoodsSupplierList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bsiName = ReqHelper.getRequestString(request, "bsiName");
		String bsiState = ReqHelper.getRequestString(request, "bsiState");
		
		String strList = this.supplierService.getGoodsSupplierList(pageIndex, pageSize, bsiName, bsiState);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateGoodsSupplier.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "goodsSupplierInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseSupplierInfo goodsSupplier = (BaseSupplierInfo)JSONObject.toBean(obj, BaseSupplierInfo.class);
		
		if(StringUtil.isNotNullOrEmpty(goodsSupplier.getBsiId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSUPPLIER_MGR, 
				"saveOrUpdateObject", lslType, "BaseSupplierInfo");
		
		this.supplierService.saveOrUpdateObject(goodsSupplier, goodsSupplier.getBsiId());
	}
	
	@RequestMapping("/getGoodsSupplierById.do")
	public void getGoodsSupplierById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bsiId = ReqHelper.getRequestString(request, "bsiId");		
		String strInfo = this.supplierService.getGoodsSupplierById(bsiId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateGoodsSupplierStateById.do")
	public void updateGoodsSupplierStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bsiId = ReqHelper.getRequestString(request, "bsiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.supplierService.updateGoodsSupplierStateById(bsiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSUPPLIER_MGR, 
				"updateGoodsSupplierStateById", ConstantLocation.LOG_Edit_STATE, "BaseSupplierInfo");

	}
	
	@RequestMapping("/delGoodsSupplierByIds.do")
	public void delGoodsSupplierByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bsiIds = ReqHelper.getRequestString(request, "bsiIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.supplierService.delObject(bsiIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSUPPLIER_MGR, 
				"delObject", ConstantLocation.LOG_DELETE, "BaseSupplierInfo");
	}
	
	@RequestMapping("/delGoodsBsiLogo.do")
	public void delGoodsBsiLogo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bsiId = ReqHelper.getRequestString(request, "bsiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.supplierService.delGoodsBsiLogo(bsiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSUPPLIER_MGR, 
				"delGoodsBsiLogo", ConstantLocation.LOG_DELETE_PIC, "BaseSupplierInfo");

	}
	
	@RequestMapping("/getBgiByBsi.do")
	public void getBgiByBsi(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bsiIds = ReqHelper.getRequestString(request, "bsiIds");

		boolean isUsed = this.supplierService.getBgiByBsi(bsiIds);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
}
